package cn.ztydata.teachingsystem.heaven.web.controller;

import cn.ztydata.teachingsystem.common.Page;
import cn.ztydata.teachingsystem.heaven.common.WebContext;
import cn.ztydata.teachingsystem.heaven.cons.GroupType;
import cn.ztydata.teachingsystem.heaven.cons.MsgTemplate;
import cn.ztydata.teachingsystem.heaven.entity.Notice;
import cn.ztydata.teachingsystem.heaven.entity.User;
import cn.ztydata.teachingsystem.heaven.service.NoticeService;
import cn.ztydata.teachingsystem.heaven.web.dto.NoticeDto;
import cn.ztydata.teachingsystem.heaven.web.wrapper.HttpSessionProxy;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResult;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResultData;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResultPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.text.MessageFormat;


/**
 * 通知控制器类
 *
 * @author lyj
 * @since 2015-3-18
 */
@Controller
@RequestMapping("/notice")
public class NoticeController extends BaseController{
    @Autowired
    private NoticeService noticeService;
    @Value("${session.key}")
    private String sessionKey;

    //<editor-fold desc="新增">
    /**
     * 新增通知
     *
     * @param noticeDto 通知Dto实体
     * @return JsonResult
     * @author lyj
     * @since 2015-3-18
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult addNotice(@Valid @RequestBody NoticeDto noticeDto) {

        noticeService.addNotice(mapper.map(noticeDto, Notice.class));

        log.debug("新增通知");
        return new JsonResult(true, "新增成功");
    }
    //</editor-fold>

    //<editor-fold desc="删除">
    /**
     * 删除通知
     *
     * @param id 通知ID
     * @return JsonResult
     * @author lyj
     * @since 2015-3-18
     */
    @RequestMapping(value = "/delete/{id}/{groupType}/{groupId}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResult deleteNotice(@PathVariable("id") long id,
                                   @PathVariable("groupType") int groupType,
                                   @PathVariable("groupId") long groupId,
                                   HttpServletRequest request) {
        Assert.isTrue(id > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "通知ID"));
        Assert.isTrue(groupType > 0 && groupType < 4, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "群组类型"));

        //获取登录用户信息
        User userInfo = (User)((HttpSessionProxy)request.getSession()).getAttribute(sessionKey);

        noticeService.deleteNotice(id, groupType, groupId, userInfo);

        log.debug("删除通知");
        return new JsonResult(true, "删除成功");
    }
    //</editor-fold>

    //<editor-fold desc="编辑">
    /**
     * 编辑通知
     *
     * @param noticeDto 通知Dto实体
     * @return JsonResult
     * @author lyj
     * @since 2015-3-18
     */
    @RequestMapping(value = "/edit", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResult editNotice(@Valid @RequestBody NoticeDto noticeDto,
                                 HttpServletRequest request) {
        Assert.notNull(noticeDto.getId(), MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "通知ID"));

        //获取登录用户信息
        User userInfo = (User)((HttpSessionProxy)request.getSession()).getAttribute(sessionKey);

        noticeService.editNotice(mapper.map(noticeDto, Notice.class), userInfo);

        log.debug("编辑通知");
        return new JsonResult(true, "编辑成功");

    }
    //</editor-fold>

    //<editor-fold desc="查询">

    /**
     * 获取通知明细
     * @param id 通知ID
     * @param groupType 群组类型
     * @param groupId 群组ID
     * @param request
     * @return JsonResultData
     * @author lyj
     * @since 2015-5-7
     */
    @RequestMapping(value = "/detail/{id}/{groupType}/{groupId}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResultData getNoticeDetail(@PathVariable("id") long id,
                                          @PathVariable("groupType") int groupType,
                                          @PathVariable("groupId") long groupId,
                                          HttpServletRequest request){
        Assert.isTrue(id > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "通知ID"));

        //获取登录用户信息
        User userInfo = (User)((HttpSessionProxy)request.getSession()).getAttribute(sessionKey);

        log.debug("获取通知明细");
        return new JsonResultData(mapper.map(noticeService.getNoticeDetail(id, groupType, groupId, userInfo), NoticeDto.class));
    }


    /**
     * 获取通知列表
     *
     * @param groupType 群组类型
     * @param groupId 群组ID
     * @param pageNum 当前页码
     * @param pageSize 每页条数
     * @return JsonResultPage
     * @author lyj
     * @since 2015-3-18
     */
    @RequestMapping(value = "/list/{groupType}/{groupId}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResultPage getNotices(@PathVariable("groupType") int groupType,
                                     @PathVariable("groupId") long groupId,
                                     @RequestParam("page") int pageNum,
                                     @RequestParam("pageSize") int pageSize,
                                     HttpServletRequest request){
        Assert.isTrue(groupType >= 0 && groupType < 4, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "群组类型"));
        Assert.isTrue(groupId > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "群组ID"));
        Assert.isTrue(pageNum > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "当前页码"));
        Assert.isTrue(pageSize > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "每页条数"));

        //获取登录用户信息
        User userInfo = (User)((HttpSessionProxy)request.getSession()).getAttribute(sessionKey);

        Page<Notice> page = noticeService.getNotices(groupType, groupId, pageNum, pageSize, userInfo);

        log.debug("获取通知列表");
        return new JsonResultPage(new Page(page.getTotal(), pageSize, pageNum, mapper.mapList(page.getItems(), NoticeDto.class)));
    }
    //</editor-fold>
}
